package cn.lichenfei.app.satoken.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 全局异常拦截
    @ExceptionHandler(value = NotLoginException.class)
    public SaResult saTokenException(NotLoginException e) {
        return SaResult.error("请先登录").setData(e.getMessage());
    }

    @ExceptionHandler(value = NotPermissionException.class)
    public SaResult saTokenException(NotPermissionException e) {
        return SaResult.error("权限不足").setData(e.getMessage());
    }

    @ExceptionHandler(value = NotRoleException.class)
    public SaResult saTokenException(NotRoleException e) {
        return SaResult.error("角色不足").setData(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public SaResult exception(Exception e) {
        return SaResult.error("系统繁忙").setData(e.getMessage());
    }

}
